/****************************************************************************/
/*This program runs regressions and exports the results to Latex            */
/****************************************************************************/

#delimit;
set more off;
set matsize 11000;
capture log close;

local pathsmr  C:\Research;
local Torpath  `pathsmr'\Tornadoes\0515Miles_CountyM;

/***************************************************************************/
/* Create natural ln version of LHS vars                                   */
/***************************************************************************/

/*Create RHS variables to be used in the regressions*/
use `Torpath'\Data\business_tornado_data_final.dta, clear;

global outcomestubs "firm emp sales firm_1y emp_1y sales_1y firm_2to3y emp_2to3y sales_2to3y firm_4plusy emp_4plusy sales_4plusy" ;
global industrystubs "all manuf retail service nonmanuf nongov noncngv public mining const trans whole agric fire" ;

foreach i of global outcomestubs { ;
foreach j of global industrystubs { ;

gen ln_`i'_`j' = ln(`i'_`j' +1);

};
};

save `Torpath'\Temp\temp.dta, replace;

/***************************************************************************/
/* Create a loop that loops through the damage levels                      */
/***************************************************************************/

local k = 1;
while `k' <= 4{;

if `k' == 1 {;
local damvar alltreat;
};

if `k' == 2 {;
local damif g1;
local damvar g1treat;
};

if `k' == 3 {;
local damif g2;
local damvar g2treat;
}; 

if `k' == 4 {;
local damif g3;
local damvar g3treat;
}; 

/****************************************************************************/
/*Create RHS variables to be used in the regressions                        */
/****************************************************************************/

use `Torpath'\Temp\temp.dta, clear;

if `k' > 1 {;
*replace treated = 0 if dam_weight_`damif' == 0; 
drop if dam_weight_`damif' == 0; 
};

/*This is where you edit it if you want shifts or not*/
*gen ydate = tor_year;
gen ydate = tornado_year_temp;

tabulate year, generate(yeardum);

gen after = (year > ydate);
gen after_t = (after*treated);
gen ia_after = (after*individual_assistance);
gen ia_t = (individual_assistance*treated);
gen ia_after_t = (after*individual_assistance*treated);


gen after_2 = (year >= ydate);
gen after_t_2 = (after_2*treated);

gen balanced_dd = 0;
replace balanced_dd = 1 if year >= ydate-3 & year <= ydate+3;

gen balanced_dd_2 = 0;
replace balanced_dd_2 = 1 if year >= ydate-4 & year <= ydate+4;

gen balanced_dd_3 = 0;
replace balanced_dd_3 = 1 if year >= ydate-4 & year <= ydate+6;

gen ydate_m4p = ydate - 4;
gen ydate_m3 = ydate - 3;
gen ydate_m2 = ydate - 2;
gen ydate_m1 = ydate - 1;
gen ydate_p0 = ydate;
gen ydate_p1 = ydate + 1;
gen ydate_p2 = ydate + 2;
gen ydate_p3 = ydate + 3;
gen ydate_p4 = ydate + 4;
gen ydate_p5 = ydate + 5;
gen ydate_p6p = ydate + 6;

gen ysm4p = (year <= ydate_m4p);
gen ysm4 = (year == ydate_m4p);
gen ysm3 = (year == ydate_m3);
gen ysm2 = (year == ydate_m2);
gen ysm1 = (year == ydate_m1);
gen ysp0 = (year == ydate_p0);
gen ysp1 = (year == ydate_p1);
gen ysp2 = (year == ydate_p2);
gen ysp3 = (year == ydate_p3);
gen ysp4 = (year == ydate_p4);
gen ysp5 = (year == ydate_p5);
gen ysp6p = (year >= ydate_p6p);

gen pre = 0;
replace pre = 1 if ysm2 == 1 | ysm3 == 1 | ysm4 == 1;

gen post = 0;
replace post = 1 if ysp0 == 1 | ysp1 == 1 | ysp2 == 1 | ysp3 == 1 | ysp4 == 1;

gen pre_2 = 0;
replace pre_2 = 1 if ysm1 == 1 | ysm3 == 1 | ysm4 == 1;
gen pre_3 = 0;
replace pre_3 = 1 if ysm1 == 1 | ysm2 == 1 | ysm4 == 1;
gen pre_4 = 0;
replace pre_4 = 1 if ysm1 == 1 | ysm2 == 1 | ysm3 == 1;
gen pre_5 = 0;
replace pre_5 = 1 if ysm1 == 1 | ysm2 == 1 | ysm3 == 1 | ysp4 == 1;

/*post_2 drops event year 0*/
gen post_2 = 0;
replace post_2 = 1 if ysp1 == 1 | ysp2 == 1 | ysp3 == 1 | ysp4 == 1;

local vlist "pre pre_2 pre_3 pre_4 pre_5 post post_2";
foreach n of local vlist{;
gen `n'_t=`n'*treated;
gen `n'_ia=`n'*individual_assistance;
gen `n'_ia_t=`n'*individual_assistance*treated;
};

drop ydate_*;
save `Torpath'\Temp\temp_program_5_`damvar'.dta, replace;

clear;
local k = `k' + 1;
};

/****************************************************************************/
/*Include intensity indicator var in the regression;                        */
/****************************************************************************/
local files "5 sizev2";

foreach y of local files{;
use `Torpath'\Temp\temp_program_`y'_alltreat.dta, clear;

/*Gen indicator variables*/
gen ef_group1= (weighted_intensity>=0 & weighted_intensity<2);
gen ef_group2= (weighted_intensity>=2 & weighted_intensity<4);
gen ef_group3= (weighted_intensity>=4 & weighted_intensity<=6);

/*New Gen indicator variables*/
gen ef_newgroup1= (weighted_intensity>=0 & weighted_intensity<3);
gen ef_newgroup2= (weighted_intensity>=3 & weighted_intensity<6);

forvalues w = 1/3{;
	gen ef_group`w'_after=ef_group`w'*after;
	gen ef_group`w'_after_2=ef_group`w'*after_2;
	gen ia_ef_group`w'=individual_assistance*ef_group`w';
	};

forvalues w = 1/2{;
	gen ef_newgroup`w'_after=ef_newgroup`w'*after;
	gen ef_newgroup`w'_after_2=ef_newgroup`w'*after_2;
	gen ia_ef_newgroup`w'=individual_assistance*ef_newgroup`w';
	};

*Gen new variables for DDD models;
local var "pre post pre_2 post_2 pre_5 after";
local groups "ef_group1 ef_group2 ef_group3";

foreach v of local var {;	
foreach g of local groups {;
	gen `v'_`g'=`v'*`g';
	gen `v'_ia_`g'=`v'*individual_assistance*`g';
	};
};

*Gen new variables for DDD models;
local var "pre post pre_2 post_2 pre_5 after";
local groups "ef_newgroup1 ef_newgroup2";

foreach v of local var {;	
foreach g of local groups {;
	gen `v'_`g'=`v'*`g';
	gen `v'_ia_`g'=`v'*individual_assistance*`g';
	};
};

gen tractid = substr(bgfips,1,14);

save `Torpath'\Temp\temp_program_`y'_alltreat_reg_analysis.dta, replace;
 
};

/****************************************************************************/
/*Run Regressions                                                           */
/****************************************************************************/

use `Torpath'\Temp\temp_program_5_alltreat_reg_analysis.dta, clear;

replace weighted_intensity = 0 if weighted_intensity==.;
gen after_weighted_intensity = after*weighted_intensity;
gen after_tint = after*weighted_intensity;
gen ia_tint = individual_assistance*weighted_intensity;
gen ia_after_tint = after*individual_assistance*weighted_intensity;

local controls "ef_group1_after ef_group2_after ef_group3_after ef_group1 ef_group2 ef_group3 after";
local newcontrols "ef_newgroup1_after ef_newgroup2_after ef_newgroup1 ef_newgroup2 after";
*local controls_2 "ef_group1_after_2 ef_group2_after_2 ef_group3_after_2  ef_group1 ef_group2 ef_group3 after_2";

local DDD_1 "pre_ia_ef_group1 pre_ia_ef_group2 pre_ia_ef_group3 
			post_ia_ef_group1 post_ia_ef_group2 post_ia_ef_group3 
			pre_ef_group1 pre_ef_group2 pre_ef_group3  post_ef_group1 post_ef_group2 post_ef_group3 
			pre post pre_ia post_ia ef_group1 ef_group2 ef_group3 individual_assistance ia_ef_group1 ia_ef_group2 ia_ef_group3";
local DDD_2 "pre_ia_ef_group1 pre_ia_ef_group2 pre_ia_ef_group3 
			post_2_ia_ef_group1 post_2_ia_ef_group2 post_2_ia_ef_group3 
			pre_ef_group1 pre_ef_group2 pre_ef_group3 post_2_ef_group1 post_2_ef_group2 post_2_ef_group3 
			pre post_2 pre_ia post_2_ia ef_group1 ef_group2 ef_group3  individual_assistance ia_ef_group1 ia_ef_group2 ia_ef_group3";
/*local DDD_3 "pre_5_ia_ef_group1 pre_5_ia_ef_group2 pre_5_ia_ef_group3 
			post_2_ia_ef_group1 post_2_ia_ef_group2 post_2_ia_ef_group3 
			pre_5_ef_group1 pre_5_ef_group2 pre_5_ef_group3 post_2_ef_group1 post_2_ef_group2 post_2_ef_group3 
			pre_5 post_2 pre_5_ia post_2_ia ef_group1 ef_group2 ef_group3 individual_assistance ia_ef_group1 ia_ef_group2 ia_ef_group3";
*/
local DDD_4 "after_ia_ef_group1 after_ia_ef_group2 after_ia_ef_group3
			ef_group1_after ef_group2_after ef_group3_after
			ia_after after  ef_group1 ef_group2 ef_group3 individual_assistance ia_ef_group1 ia_ef_group2 ia_ef_group3";

local DDD_new4 "after_ia_ef_newgroup1 after_ia_ef_newgroup2
			ef_newgroup1_after ef_newgroup2_after
			ia_after after  ef_newgroup1 ef_newgroup2  individual_assistance ia_ef_newgroup1 ia_ef_newgroup2";
	
	

/*******************************************************************************/
/*Summary Table                                                                */	
/*******************************************************************************/

sum firm_all if individual_assistance == 1 & ysm1 == 1;
sum firm_all if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum firm_all if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum firm_all if individual_assistance == 0 & ysm1 == 1;
sum firm_all if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum firm_all if individual_assistance == 0 & treated ==0 & ysm1 == 1;

sum emp_all if individual_assistance == 1 & ysm1 == 1;
sum emp_all if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum emp_all if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum emp_all if individual_assistance == 0 & ysm1 == 1;
sum emp_all if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum emp_all if individual_assistance == 0 & treated ==0 & ysm1 == 1;

sum sales_all if individual_assistance == 1 & ysm1 == 1;
sum sales_all if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum sales_all if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum sales_all if individual_assistance == 0 & ysm1 == 1;
sum sales_all if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum sales_all if individual_assistance == 0 & treated ==0 & ysm1 == 1;

sum firm_1y_all if individual_assistance == 1 & ysm1 == 1;
sum firm_1y_all if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum firm_1y_all if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum firm_1y_all if individual_assistance == 0 & ysm1 == 1;
sum firm_1y_all if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum firm_1y_all if individual_assistance == 0 & treated ==0 & ysm1 == 1;

sum emp_1y_all if individual_assistance == 1 & ysm1 == 1;
sum emp_1y_all if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum emp_1y_all if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum emp_1y_all if individual_assistance == 0 & ysm1 == 1;
sum emp_1y_all if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum emp_1y_all if individual_assistance == 0 & treated ==0 & ysm1 == 1;

sum sales_1y_all if individual_assistance == 1 & ysm1 == 1;
sum sales_1y_all if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum sales_1y_all if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum sales_1y_all if individual_assistance == 0 & ysm1 == 1;
sum sales_1y_all if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum sales_1y_all if individual_assistance == 0 & treated ==0 & ysm1 == 1;

gen manu_share = emp_manu/emp_all;

sum manu_share if individual_assistance == 1 & ysm1 == 1;
sum manu_share if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum manu_share if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum manu_share if individual_assistance == 0 & ysm1 == 1;
sum manu_share if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum manu_share if individual_assistance == 0 & treated ==0 & ysm1 == 1;


sum firm_manu if individual_assistance == 1 & ysm1 == 1;
sum firm_manu if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum firm_manu if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum firm_manu if individual_assistance == 0 & ysm1 == 1;
sum firm_manu if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum firm_manu if individual_assistance == 0 & treated ==0 & ysm1 == 1;

sum firm_nonmanu if individual_assistance == 1 & ysm1 == 1;
sum firm_nonmanu if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum firm_nonmanu if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum firm_nonmanu if individual_assistance == 0 & ysm1 == 1;
sum firm_nonmanu if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum firm_nonmanu if individual_assistance == 0 & treated ==0 & ysm1 == 1;


sum emp_manu if individual_assistance == 1 & ysm1 == 1;
sum emp_manu if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum emp_manu if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum emp_manu if individual_assistance == 0 & ysm1 == 1;
sum emp_manu if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum emp_manu if individual_assistance == 0 & treated ==0 & ysm1 == 1;

sum emp_nonmanu if individual_assistance == 1 & ysm1 == 1;
sum emp_nonmanu if individual_assistance == 1 & treated ==1 & ysm1 == 1;
sum emp_nonmanu if individual_assistance == 1 & treated ==0 & ysm1 == 1;
sum emp_nonmanu if individual_assistance == 0 & ysm1 == 1;
sum emp_nonmanu if individual_assistance == 0 & treated ==1 & ysm1 == 1;
sum emp_nonmanu if individual_assistance == 0 & treated ==0 & ysm1 == 1;

sum emp_all if individual_assistance == 1 & ef_group3 ==1 & ysm1 == 1;
sum emp_all if individual_assistance == 1 & treated ==1 & ysm1 == 1;

display "summary stats by treatment group";
sum firm_all if treated ==1 & ysm1 == 1 & ef_group1 ==1;
sum firm_all if treated ==1 & ysm1 == 1 & ef_group2 ==1;
sum firm_all if treated ==1 & ysm1 == 1 & ef_group3 ==1;

sum emp_all if treated ==1 & ysm1 == 1 & ef_group1 ==1;
sum emp_all if treated ==1 & ysm1 == 1 & ef_group2 ==1;
sum emp_all if treated ==1 & ysm1 == 1 & ef_group3 ==1;

sum sales_all if treated ==1 & ysm1 == 1 & ef_group1 ==1;
sum sales_all if treated ==1 & ysm1 == 1 & ef_group2 ==1;
sum sales_all if treated ==1 & ysm1 == 1 & ef_group3 ==1;

sum manu_share if treated ==1 & ysm1 == 1 & ef_group1 ==1;
sum manu_share if treated ==1 & ysm1 == 1 & ef_group2 ==1;
sum manu_share if treated ==1 & ysm1 == 1 & ef_group3 ==1;

sum firm_all if treated ==1 & ysm1 == 1 & ef_group1 ~=1;
sum firm_all if treated ==1 & ysm1 == 1 & ef_group2 ~=1;
sum firm_all if treated ==1 & ysm1 == 1 & ef_group3 ~=1;

sum emp_all if treated ==1 & ysm1 == 1 & ef_group1 ~=1;
sum emp_all if treated ==1 & ysm1 == 1 & ef_group2 ~=1;
sum emp_all if treated ==1 & ysm1 == 1 & ef_group3 ~=1;

sum manu_share if treated ==1 & ysm1 == 1 & ef_group1 ~=1;
sum manu_share if treated ==1 & ysm1 == 1 & ef_group2 ~=1;
sum manu_share if treated ==1 & ysm1 == 1 & ef_group3 ~=1;

estimates drop _all;


/*******************************************************************************/
/*Tables 2 and 5 and Appendix Table16: DD with clustering - All                */	
/*******************************************************************************/

local depvar "firm emp sales";

foreach l of local depvar{;

areg ln_`l'_all after_t after treated if balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_all_pooled;

areg ln_`l'_all after_t after treated if individual_assistance == 1 & balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_IA_pooled;

areg ln_`l'_all after_t after treated if individual_assistance == 0 & balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_nonIA_pooled;

areg ln_`l'_all after_weighted_intensity after weighted_intensity if balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_all_int;

boottest after_weighted_intensity , reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[after_weighted_intensity] / `r(t)';
display "`bootse'";

display "Stacked DD";
areg ln_`l'_all after_weighted_intensity weighted_intensity year#tornado_id if balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);


areg ln_`l'_all after_weighted_intensity after weighted_intensity if individual_assistance == 1 & balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_IA_intensity;

boottest after_weighted_intensity , reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[after_weighted_intensity] / `r(t)';
display "`bootse'";


display "Stacked DD";
areg ln_`l'_all after_weighted_intensity after weighted_intensity year#tornado_id if individual_assistance == 1 & balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);

areg ln_`l'_all after_weighted_intensity after weighted_intensity if individual_assistance == 0 & balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_nonIA_int;

boottest after_weighted_intensity , reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[after_weighted_intensity] / `r(t)';
display "`bootse'";

display "Stacked DD";
areg ln_`l'_all after_weighted_intensity after weighted_intensity year#tornado_id if individual_assistance == 0 & balanced_dd_2 ==1 & ysp0!=1, vce (cluster tractid) absorb(bgfips);

areg ln_`l'_all `controls'  if balanced_dd_2 ==1 & ysp0!=1,  vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_all;

boottest {ef_group1_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group1_after] / `r(t)';
display "`bootse'";

boottest {ef_group2_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group2_after] / `r(t)';
display "`bootse'";

boottest {ef_group3_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group3_after] / `r(t)';
display "`bootse'";

display "Stacked DD";
areg ln_`l'_all `controls' year#tornado_id  if balanced_dd_2 ==1 & ysp0!=1,  vce (cluster tractid) absorb(bgfips);

areg ln_`l'_all `controls'  if individual_assistance == 1 & balanced_dd_2 ==1 & ysp0!=1,  vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_IA;

boottest {ef_group1_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group1_after] / `r(t)';
display "`bootse'";

boottest {ef_group2_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group2_after] / `r(t)';
display "`bootse'";

boottest {ef_group3_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group3_after] / `r(t)';
display "`bootse'";

display "Stacked DD";
areg ln_`l'_all `controls' year#tornado_id  if individual_assistance == 1 & balanced_dd_2 ==1 & ysp0!=1,  vce (cluster tractid) absorb(bgfips);

areg ln_`l'_all `controls'  if individual_assistance == 0 & balanced_dd_2 ==1 & ysp0!=1,  vce (cluster tractid) absorb(bgfips);
eststo model_ln_`l'_nonIA;

boottest {ef_group1_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group1_after] / `r(t)';
display "`bootse'";

boottest {ef_group2_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group2_after] / `r(t)';
display "`bootse'";

boottest {ef_group3_after}, reps(9999) gridpoints(10) bootcluster(tractid);
local bootse = _b[ef_group3_after] / `r(t)';
display "`bootse'";


display "Stacked DD";
areg ln_`l'_all `controls' year#tornado_id  if individual_assistance == 0 & balanced_dd_2 ==1 & ysp0!=1,  vce (cluster tractid) absorb(bgfips);


};


global top "keep(after_t) star(* .10 ** .05 *** .01) se(3) b(3) nonotes coeflabels(after_t "After Tornado $\times$ Hit") page(dcolumn) 
booktabs order(after_t) stats(, labels() fmt(3)) mtitles ("IA" "non-IA" "IA" "non-IA" "IA" "non-IA") nocons replace width(\hsize) postfoot(\end{tabular*} }) prefoot("") varwidth(25) eqlabels("") 
mgroups("Log(Firms)" "Log(Employment)" "Log(Sales)", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) )";

global middle "keep(after_weighted_intensity) star(* .10 ** .05 *** .01) se(3) b(3) nonotes page(dcolumn)  coeflabels(after_weighted_intensity "After Tornado $\times$ Damage Level") 
booktabs order(after_weighted_intensity) stats(, labels() fmt(3))  nomtitles nocons width(\hsize) postfoot(\end{tabular*} }) prefoot("") append collabels(none) 
 prehead(`"{"' `"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"' \begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{@E}{c}})  nonumbers eqlabels("")";

global end "keep(ef_group1_after ef_group2_after ef_group3_after) star(* .10 ** .05 *** .01) page(dcolumn)  
se(3) b(3) nonotes coeflabels (ef_group1_after "After Tornado $\times$ Low" ef_group2_after "After Tornado $\times$ Medium" ef_group3_after "After Tornado $\times$ High") 
booktabs stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) nomtitles nocons append width(\hsize) nonumbers 
prehead(`"{"' `"\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}"' \begin{tabular*}{\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l*{@E}{c}}) eqlabels("")";


*load_reg parta;

esttab model_ln_firm_all_pooled model_ln_firm_IA_pooled model_ln_firm_nonIA_pooled model_ln_emp_all_pooled model_ln_emp_IA_pooled model_ln_emp_nonIA_pooled  model_ln_sales_all_pooled model_ln_sales_IA_pooled model_ln_sales_nonIA_pooled 
model_ln_sales_IA_pooled model_ln_sales_nonIA_pooled using "`Torpath'\Tables\Formated\Tables2and5_DD.tex", ${top} posthead("\midrule \textbf{Panel A: Pooled}\\");

*load_reg partb;
esttab model_ln_firm_all_int model_ln_firm_IA_intensity model_ln_firm_nonIA_int model_ln_emp_all_int model_ln_emp_IA_intensity model_ln_emp_nonIA_int model_ln_sales_all_int model_ln_sales_IA_intensity model_ln_sales_nonIA_int
model_ln_sales_IA_intensity model_ln_sales_nonIA_int using "`Torpath'\Tables\Formated\Tables2and5.tex", ${middle} posthead("\addlinespace \textbf{Panel B: Heterogenous Damage Level}\\");

*load_reg partc;

esttab model_ln_firm_all model_ln_firm_IA model_ln_firm_nonIA model_ln_emp_all model_ln_emp_IA model_ln_emp_nonIA model_ln_sales_all model_ln_sales_IA model_ln_sales_nonIA 
model_ln_sales_nonIA using "`Torpath'\Tables\Formated\Tables2and5.tex", ${end} posthead("\addlinespace \textbf{Panel C: Binned Damage Levels}\\");

estimates drop _all;


/***********************************************************************************/
/*Tables 5 and Appendix Tables 11 and 16: DDD-regular                              */
/***********************************************************************************/

local depvar "firm emp sales";

foreach l of local depvar{;
areg ln_`l'_all ia_after_t ia_t ia_after after_t after treated individual_assistance  yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo model_ln_`l'_DDD_pooled;
};

esttab  model_ln_firm_DDD_pooled  model_ln_emp_DDD_pooled  model_ln_sales_DDD_pooled using "`Torpath'\Tables\Formated\Table5_DDD.tex", replace booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_t)  width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_t  "IA Grant $\times$ After Tornado $\times$ Treated");


foreach l of local depvar{;
areg ln_`l'_all ia_after_tint ia_tint ia_after after_tint after weighted_intensity individual_assistance  yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo model_ln_`l'_DDD_int;

boottest ia_after_tint , reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[ia_after_tint] / `r(t)';
display "`bootse'";

display "stacked DDD";
areg ln_`l'_all ia_after_tint ia_tint ia_after after_tint after weighted_intensity individual_assistance  year#tornado_id yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 

};

esttab  model_ln_firm_DDD_int  model_ln_emp_DDD_int  model_ln_sales_DDD_int using "`Torpath'\Tables\Formated\Table5_DDD.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_tint)  width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_tint  "IA Grant $\times$ After Tornado $\times$ Weight Treated");


foreach l of local depvar{;
areg ln_`l'_all `DDD_4' yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo model_ln_`l'_DDD_het;

boottest {after_ia_ef_group1}, reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[after_ia_ef_group1] / `r(t)';
display "`bootse'";

boottest {after_ia_ef_group2}, reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[after_ia_ef_group2] / `r(t)';
display "`bootse'";

boottest {after_ia_ef_group3}, reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[after_ia_ef_group3] / `r(t)';
display "`bootse'";


display "stacked DDD";
areg ln_`l'_all `DDD_4' year#tornado_id yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 

};

esttab  model_ln_firm_DDD_het  model_ln_emp_DDD_het  model_ln_sales_DDD_het using "`Torpath'\Tables\Formated\Table5_DDD.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(after_ia_ef_group1 after_ia_ef_group2 after_ia_ef_group3)  width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (after_ia_ef_group1 "IA Grant $\times$ After Tornado $\times$ Low" after_ia_ef_group2 "IA Grant $\times$ After Tornado $\times$ Medium" after_ia_ef_group3 "IA Grant $\times$ After Tornado $\times$ High");

estimates drop _all;


/*******************************************************************************/
/*******************************************************************************/
/*Heterogeneity Test: Tables 6, 9, 10, and 11                                  */
/*******************************************************************************/
/*******************************************************************************/

/*******************************************************************************/
/*Table 6 Panel C: Firm Size                                                   */
/*******************************************************************************/

use `Torpath'\Temp\temp_program_sizev2_alltreat_reg_analysis.dta, clear;

gen ia_t = (individual_assistance*treated);

replace weighted_intensity = 0 if weighted_intensity==.;
gen after_weighted_intensity = after*weighted_intensity;
gen after_tint = after*weighted_intensity;
gen ia_tint = individual_assistance*weighted_intensity;
gen ia_after_tint = after*individual_assistance*weighted_intensity;


sum emp_lt3_all if individual_assistance == 1 & treated ==1 & ysm1 == 1;

local size2 "lt3 gt3lt7 gt7" ;

local depvar "firm emp sales";

/*logs*/
foreach x of local size2{;
foreach l of local depvar{;
areg ln_`l'_`x'_all ia_after_t ia_t ia_after after_t after treated individual_assistance  yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`l'_DDD_p_`x'_v2;

boottest ia_after_t  , reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[ia_after_t] / `r(t)';
display "`bootse'";
};
};

esttab  ln_firm_DDD_p_lt3_v2  ln_emp_DDD_p_lt3_v2  ln_sales_DDD_p_lt3_v2 using "`Torpath'\Tables\Formated\Table6_other.tex", replace booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_t) refcat(ia_after_t "\emph{lt3_v2}", nolabel)  width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_t  "IA Grant $\times$ After Tornado $\times$ Treated");


esttab  ln_firm_DDD_p_gt3lt7_v2  ln_emp_DDD_p_gt3lt7_v2  ln_sales_DDD_p_gt3lt7_v2 using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_t) refcat(ia_after_t "\emph{gt3lt7_v2}", nolabel)  width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_t  "IA Grant $\times$ After Tornado $\times$ Treated");

esttab  ln_firm_DDD_p_gt7_v2  ln_emp_DDD_p_gt7_v2  ln_sales_DDD_p_gt7_v2 using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_t) refcat(ia_after_t "\emph{gt7_v2}", nolabel)  width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_t  "IA Grant $\times$ After Tornado $\times$ Treated");


foreach x of local size2{;
foreach l of local depvar{;
areg ln_`l'_`x'_all ia_after_tint ia_tint ia_after after_tint after weighted_intensity individual_assistance  yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`l'_DDD_i_`x'_v2;

boottest ia_after_tint , reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[ia_after_tint] / `r(t)';
display "`bootse'";
};
};

foreach x of local size2{;
esttab  ln_firm_DDD_i_`x'_v2 ln_emp_DDD_i_`x'_v2 ln_sales_DDD_i_`x'_v2 using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_tint)  refcat(ia_after_tint "\emph{`x'_continuous_v2}", nolabel)  width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_tint  "IA Grant $\times$ After Tornado $\times$ Weight Treated");
};


foreach x of local size2{;
foreach l of local depvar{;
areg ln_`l'_`x'_all `DDD_4' yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`l'_DDD_`x'_v2;
};
};

foreach x of local size2{;
esttab ln_firm_DDD_`x'_v2 ln_emp_DDD_`x'_v2 ln_sales_DDD_`x'_v2 using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(after_ia_ef_group3) refcat(after_ia_ef_group3 "\emph{`x'_v2}", nolabel)  width(1\hsize) 
 nodepvars varwidth(20) nonumbers nonotes mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)")
compress coeflabels (after_ia_ef_group1 "IA Grant $\times$ After Tornado $\times$ Low" after_ia_ef_group2 "IA Grant $\times$ After Tornado $\times$ Medium" after_ia_ef_group3 "IA Grant $\times$ After Tornado $\times$ High");
};

estimates drop _all;


/*******************************************************************************/
/*Tables 6 Panel A, and Appendix Tables 9 and 10: By Industry                  */
/*******************************************************************************/

local industry "nonmanuf manuf retail const service public mining const trans whole agric fire" ;

use `Torpath'\Temp\temp_program_5_alltreat_reg_analysis.dta, clear;

capture drop ia_t;
gen ia_t = 0;
replace ia_t = 1 if individual_assistance == 1 & treated ==1; 
replace weighted_intensity = 0 if weighted_intensity==.;
gen after_weighted_intensity = after*weighted_intensity;
gen after_tint = after*weighted_intensity;
gen ia_tint = individual_assistance*weighted_intensity;
gen ia_after_tint = after*individual_assistance*weighted_intensity;

/*logs*/
foreach p of local industry{;
foreach l of local depvar{;
areg ln_`l'_`p' ia_after_t ia_t ia_after after_t after treated individual_assistance  yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`l'_DDD_p_`p'_v2;

boottest ia_after_t  , reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[ia_after_t] / `r(t)';
display "`bootse'";
};
};

foreach p of local industry{;
esttab  ln_firm_DDD_p_`p'_v2  ln_emp_DDD_p_`p'_v2  ln_sales_DDD_p_`p'_v2 using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_t) refcat(ia_after_t "\emph{`p'_v2}", nolabel) width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_t  "IA Grant $\times$ After Tornado $\times$ Treated");
};


foreach p of local industry{;
foreach l of local depvar{;
areg ln_`l'_`p' ia_after_tint ia_tint ia_after after_tint after weighted_intensity individual_assistance  yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`l'_DDD_i_`p'_v2;

boottest ia_after_tint , reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[ia_after_tint] / `r(t)';
display "`bootse'";
};
};

foreach p of local industry{;
esttab  ln_firm_DDD_i_`p'_v2  ln_emp_DDD_i_`p'_v2  ln_sales_DDD_i_`p'_v2 using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_tint) refcat(ia_after_tint  "\emph{`p'_v2_continuous}", nolabel) width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_tint   "IA Grant $\times$ After Tornado $\times$ Treated");
};

foreach p of local industry{;
foreach l of local depvar{;
areg ln_`l'_`p' `DDD_4' yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`l'_DDD_`p'_r;

boottest {after_ia_ef_group1}, reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[after_ia_ef_group1] / `r(t)';
display "`bootse'";

boottest {after_ia_ef_group2}, reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[after_ia_ef_group2] / `r(t)';
display "`bootse'";

boottest {after_ia_ef_group3}, reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[after_ia_ef_group3] / `r(t)';
display "`bootse'";
};
};

foreach p of local industry{;
esttab ln_firm_DDD_`p'_r ln_emp_DDD_`p'_r ln_sales_DDD_`p'_r using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(after_ia_ef_group1 after_ia_ef_group2 after_ia_ef_group3) refcat(after_ia_ef_group3 "\emph{`p'_v2_binned}", nolabel)  width(1\hsize) 
 nodepvars varwidth(20) nonumbers nonotes mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)")
compress coeflabels (after_ia_ef_group3 "IA Grant $\times$ After Tornado $\times$ High");
};


/*******************************************************************************/
/*Table 6 Panel B: By Age                                                      */
/*******************************************************************************/

local outcomestubs "firm_1y emp_1y sales_1y firm_4plusy emp_4plusy sales_4plusy" ;
local length "1y 4plusy";

use `Torpath'\Temp\temp_program_5_alltreat_reg_analysis.dta, clear;

capture drop ia_t;
gen ia_t = 0;
replace ia_t = 1 if individual_assistance == 1 & treated ==1; 
replace weighted_intensity = 0 if weighted_intensity==.;
gen after_weighted_intensity = after*weighted_intensity;
gen after_tint = after*weighted_intensity;
gen ia_tint = individual_assistance*weighted_intensity;
gen ia_after_tint = after*individual_assistance*weighted_intensity;


/*logs*/
foreach s of local outcomestubs{;
areg ln_`s'_all ia_after_t ia_t ia_after after_t after treated individual_assistance  yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`s'_DDD_r_pool;

boottest ia_after_t  , reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[ia_after_t] / `r(t)';
display "`bootse'";
};

foreach b of local length{;
esttab  ln_firm_`b'_DDD_r_pool  ln_emp_`b'_DDD_r_pool  ln_sales_`b'_DDD_r_pool using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_t) refcat(ia_after_t "\emph{`b'}", nolabel)  width(1\hsize) 
 nonumbers nodepvars nonotes varwidth(20) mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)")
compress coeflabels (ia_after_t "IA Grant $\times$ After Tornado $\times$ treated");
};


foreach s of local outcomestubs{;
areg ln_`s'_all ia_after_tint ia_tint ia_after after_tint after weighted_intensity individual_assistance  yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`s'_DDD_r_int;

boottest ia_after_tint , reps(9999) gridpoints(10) bootcluster(tornado_id);
local bootse = _b[ia_after_tint ] / `r(t)';
display "`bootse'";
};

foreach b of local length{;
esttab  ln_firm_`b'_DDD_r_int ln_emp_`b'_DDD_r_int ln_sales_`b'_DDD_r_int using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) 
page(dcolumn) star(* .10 ** .05 *** .01) keep(ia_after_tint) refcat(ia_after_tint "\emph{`b'}", nolabel)  width(1\hsize) 
mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)") nodepvars varwidth(20) nonotes
compress coeflabels (ia_after_tint  "IA Grant $\times$ After Tornado $\times$ Weight Treated");
};

foreach s of local outcomestubs{;
areg ln_`s'_all `DDD_4' yeardum* if balanced_dd_2 == 1 & ysp0!=1 ,  vce (cluster tornado_id) absorb(bgfips); 
eststo ln_`s'_DDD_r;
};

foreach b of local length{;
esttab  ln_firm_`b'_DDD_r  ln_emp_`b'_DDD_r  ln_sales_`b'_DDD_r using "`Torpath'\Tables\Formated\Table6_other.tex", append booktabs unstack se(3) b(3) stats(r2 N,labels("R-squared" "Observations") fmt(3 0)) page(dcolumn) star(* .10 ** .05 *** .01) keep(after_ia_ef_group3) refcat(after_ia_ef_group3 "\emph{`b'}", nolabel)  width(1\hsize) 
 nonumbers nodepvars nonotes varwidth(20) mtitles ("Log(Firms)" "Log(Employment)" "Log(Sales)")
compress coeflabels (after_ia_ef_group3 "IA Grant $\times$ After Tornado $\times$ High");
};

